package com.kgc.scd.controller;

import com.kgc.scd.bean.NotifyProArrive;
import com.kgc.scd.bean.Product;
import com.kgc.scd.bean.User;
import com.kgc.scd.service.NotifyProArriveService;
import com.kgc.scd.service.ProductService;
import com.kgc.scd.service.UserService;
import com.kgc.scd.service.feign.SendProstockRemindService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created On : 2022/6/16.
 * <p>
 * Author     : nainai
 * <p>
 * Description: 商品库存操作入口
 */
@Slf4j
@RestController
public class ProductStockController {

    @Autowired
    private ProductService productService;

    @Autowired
    private NotifyProArriveService notifyProArriveService;

    @Autowired
    private UserService userService;

    @Autowired(required = false)
    private SendProstockRemindService sendProstockRemindService;

    /**
     * @author : nainai
     * @date   : 2022/6/16
     * @param  : [com.kgc.scd.util.UserAnno, java.lang.String]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 订阅商品到货提醒
     */
    @PostMapping("/setArrivalRemind")
    public Map<String, String> setArrivalRemind(@RequestParam String userId, @RequestParam String proId) {
        Product product = productService.queryProductById(proId);
        Map<String, String> map = new HashMap<>();

        if (product == null) {
            map.put("result", "该商品不存在");
            return map;
        }

        // 校验库存是否为空
        if (product.getProStock() != 0) {
            map.put("result", "库存不为空");
            return map;
        }

        // 更新到货提醒表前，先查询是否已经订阅过
        if (notifyProArriveService.checkReminded(userId, proId)) {
            map.put("result", "已经订阅过此商品");
            return map;
        }

        User user = userService.queryUserById(userId);
        if (user.getUserEmail() == null) {
            map.put("result", "邮箱不能为空");
            return map;
        }

        // 更新到货提醒表
        NotifyProArrive notifyProArrive = NotifyProArrive
                .builder()
                .proId(proId)
                .userId(userId)
                .status(0)
                .build();
        if (notifyProArriveService.addArrivalRemind(notifyProArrive)) {
            map.put("result", "true");
        }
        return map;
    }

    /**
     * @author : nainai
     * @date   : 2022/6/16
     * @param  : [com.kgc.scd.util.UserAnno]
     * @return : com.kgc.scd.util.RequestResult<java.util.List<com.kgc.scd.bean.Product>>
     * @description : 查询用户已订阅到货商品
     */
    @GetMapping("/queryUserArrivalRemind")
    public List<Product> queryUserArrivalRemind(@RequestParam String userId) {
        return notifyProArriveService.queryUserRemindProduct(userId);
    }

    /**
     * @author : nainai
     * @date   : 2022/6/16
     * @param  : [java.lang.String, java.lang.Integer]
     * @return : java.util.Map<java.lang.String,java.lang.String>
     * @description : 添加商品库存
     */
    @PostMapping("/insertProductStock")
    public Map<String, String> insertProductStock(@RequestParam String proId, @RequestParam Integer stock) {
        log.info("商品：{}，开始添加库存：{}", proId, stock);

        // 定义返回结果集合
        Map<String, String> map = new HashMap<>();

        // 校验参数是否正确
        Product product = productService.queryProductById(proId);
        if (!(product != null && stock > 0)) {
            map.put("result", "参数有误，添加库存失败！");
            return map;
        }

        // 调用业务接口，开始添加库存
        if (productService.addProductStock(proId, stock)) {
            if (product.getProState() != 0) {
                // 添加库存成功，给订阅用户发送消息
                log.info("加库存成功，开始给用户发送消息！");
                sendProstockRemindService.sendProStockRemindMsg(proId);
            }

            map.put("result", "true");
            return map;
        }
        map.put("result", "库存添加失败！");
        return map;
    }
}
